function [U_all,U_all_2,M_all] = MBB(L,res,res_ident,instr,nBoot)

    U_hat       = res;
    U_hat_2     = res_ident;
    M           = instr;
    N           = size(res,2);
    T           = size(res,1);
    J           = ceil(T/L);
    UBlocks     = zeros(L,N,T-L+1);
    UBlocks_2   = zeros(L,N,T-L+1);
    MBlocks     = zeros(L,1,T-L+1);
    
    for ii = 1:T-L+1
        UBlocks(:,:,ii)     = U_hat(ii:L+ii-1,:);
        UBlocks_2(:,:,ii)   = U_hat_2(ii:L+ii-1,:);
        MBlocks(:,:,ii)     = M(ii:L+ii-1,:);
    end
    
    % center the bootstrapped VAR errors and proxy variables
    centering   = zeros(L,N);
    centering_2 = zeros(L,N);
    Mcentering  = zeros(L,1);
    
    for ii = 1:L
        centering(ii,:)     = mean(U_hat(ii:T-L+ii,:),1);
        centering_2(ii,:)   = mean(U_hat_2(ii:T-L+ii,:),1);
        Mcentering(ii,:)    = mean(M(ii:T-L+ii,:),1);
    end
    
    centering   = repmat(centering,[J,1]);
    centering   = centering(1:T,:);
    centering_2 = repmat(centering_2,[J,1]);
    centering_2 = centering_2(1:T,:);
    Mcentering  = repmat(Mcentering,[J,1]);
    Mcentering  = Mcentering(1:T,:);

    U_all   = zeros(T,N,nBoot);
    U_all_2 = zeros(T,N,nBoot);
    M_all   = zeros(T,1,nBoot);
    
    for boot = 1:nBoot
        % draw bootstrapped VAR residuals and proxies
        index = ceil((T - L + 1)*rand(J,1));
        U_boot      = zeros(J*L,N);
        U_boot_2    = zeros(J*L,N);
        M_boot      = zeros(J*L,1);
        for j = 1:J
            U_boot(1+L*(j-1):L*j,:)     = UBlocks(:,:,index(j,1));
            U_boot_2(1+L*(j-1):L*j,:)   = UBlocks_2(:,:,index(j,1));
            M_boot(1+L*(j-1):L*j,:)     = MBlocks(:,:,index(j,1));
        end
        U_boot      = U_boot(1:T,:);
        U_boot_2    = U_boot_2(1:T,:);
        M_boot      = M_boot(1:T,:);

        % center the VAR residuals and proxies
        U_boot      = U_boot - centering;
        U_boot_2    = U_boot_2 - centering_2;
        M_boot      = M_boot - Mcentering;
        
        U_all(:,:,boot)     = U_boot;
        U_all_2(:,:,boot)   = U_boot_2;
        M_all(:,:,boot)     = M_boot;
    end
    
end